package com.zw.stock_sell_storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zw.stock_sell_storage.dto.MyPageDto;
import com.zw.stock_sell_storage.dto.SupplierDto;
import com.zw.stock_sell_storage.mapper.SupplierMapper;
import com.zw.stock_sell_storage.service.ISupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, SupplierDto> implements ISupplierService {
    @Autowired
    SupplierMapper supplierMapper;

    @Override
    public Page<SupplierDto> querySupplierList(MyPageDto<SupplierDto> myPageDto) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (myPageDto.getData().getSupplierName()!=null&&!"".equals(myPageDto.getData().getSupplierName())){
            queryWrapper.like("supplier_name",myPageDto.getData().getSupplierName());

        }
        if (myPageDto.getData().getSupplierBusinessScope()!=null&&!"".equals(myPageDto.getData().getSupplierBusinessScope())){
            queryWrapper.like("supplier_business_scope",myPageDto.getData().getSupplierBusinessScope());

        }

        return supplierMapper.selectPage(new Page<>(myPageDto.getCurrentPage(),myPageDto.getPageSize()),queryWrapper);
    }

    @Override
    public SupplierDto querySupplierOne(int supplierId) {
        return supplierMapper.selectById(supplierId);
    }

    @Override
    @Transactional
    public boolean updateSupplier(SupplierDto supplierDto) {

        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("supplier_id",supplierDto.getSupplierId());
        supplierMapper.update(supplierDto,updateWrapper);
        return true;
    }

    @Override
    @Transactional
    public boolean addSupplier(SupplierDto supplierDto) {
        supplierMapper.insert(supplierDto);
        return true;
    }

    @Override
    public boolean importSupplierList(List<SupplierDto> supplierList) {
        supplierMapper.insertSupplierList(supplierList);
        return true;
    }

    @Override
    public List<SupplierDto> queryAllSupplierList() {
        return supplierMapper.selectList(null);
    }
}
